package meterMonitor.service;

import meterMonitor.constant.Constant;
import meterMonitor.model.LinePO;
import meterMonitor.model.LineRecPO;
import meterMonitor.model.LotnumRecPO;
import meterMonitor.model.SlotPO;
import meterMonitor.model.bo.RealtimeDeviceDataBO;
import meterMonitor.model.bo.RealtimeLineDataBO;
import meterMonitor.model.bo.RealtimeSlotDataBO;
import meterMonitor.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 实时数据查询
 */
@Service
public class RealtimeDataService {

    @Autowired
    private LineService lineService;
    @Autowired
    private SlotService slotService;
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private LotnumRecService lotnumRecService;
    @Autowired
    private LineRecService lineRecService;

    public RealtimeLineDataBO getRealtimeLineData(Integer lineId) {
        LinePO lineById = lineService.getLineById(lineId);
        RealtimeLineDataBO lineData = new RealtimeLineDataBO();
        // 复制属性
        BeanUtils.copyProperties(lineById, lineData);
        // 查询当前产线关联的工单
        LotnumRecPO lotnumRec = lotnumRecService.queryCurrentLotnumRecByLineId(lineId);
        if (Objects.nonNull(lotnumRec)) {
            BeanUtils.copyProperties(lotnumRec, lineData);
        }
        List<SlotPO> slots = slotService.querySlotByLineId(lineId);
        List<RealtimeSlotDataBO> slotDatas = new ArrayList<>();
        Integer slotStatus = 0;
        for (SlotPO s : slots) {
            RealtimeSlotDataBO slotData = new RealtimeSlotDataBO();
            BeanUtils.copyProperties(s, slotData);

            List<RealtimeDeviceDataBO> deviceDatas = deviceInfoService.queryRealtimeDeviceDatas(s.getId());

            // 遍历设备数据，如果为空则表示离线
            for (RealtimeDeviceDataBO data : deviceDatas) {
                // 查询数据为空，说明设备离线
                if (Objects.isNull(data.getDataValue())) {
                    slotStatus = -1;
                    lineData.setLineStatus(slotStatus);
                    data.setDeviceStatus(-1); // 离线
                } else if (data.getIsOverLimit() == 1) { // 超标
                    // 非离线状态可以设置为超标
                    if (slotStatus != -1) {
                        slotStatus = 1;
                    }
                    data.setDeviceStatus(1);
                } else {
                    data.setDeviceStatus(0); // 正常
                }
            }

            slotData.setSlotStatus(slotStatus);

            if (Objects.isNull(lineData.getLineStatus())) {
                lineData.setLineStatus(slotStatus);
            }

            // 设置状态级别
            if (lineData.getLineStatus() != -1) { // 异常
                if (slotStatus == 1) {
                    lineData.setLineStatus(1);
                }
                if (slotStatus == 0) {
                    if (Objects.isNull(lineData.getLineStatus())) {
                        lineData.setLineStatus(0);
                    }
                }
            }
            slotData.setDeviceDatas(deviceDatas);
            slotDatas.add(slotData);
        }

        lineData.setSlotDatas(slotDatas);
        Date date = new Date();
        LineRecPO lineRec = lineRecService.getTypeRec(lineId, Constant.MAINTENANCE);


        try {
            if (lineRec != null && lineRec.getEndTime() != null) {
                int day = DateUtil.longOfTwoDate(date, lineRec.getEndTime());
                // lineData.setPeriodNum(lineById.getPeriod() - day);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return lineData;
    }


}
